import axios from "axios";

const state = {
  rectifySource: [],
  inspectSource: [],
  attention: {
    clientSelected: [],
    clientStroked: "",
    rangeSelected: "",
    detailType: "",
    lastChange: 0,
    // clientChecked: [],
  },
  inspectSummary: {
    conditions: [],
  },
  rectifySummary: {
    conditions: [],
  },
};

const mutations = {
  SET_RECTIFY_SOURCE: (state, data) => {
    state.rectifySource = data;
  },
  SET_INSPECT_SOURCE: (state, data) => {
    state.inspectSource = data;
  },
  // MULTIPLE_SELECT_CLIENTS: (state, data) => {
  //   state.clientChecked = data;
  // },
  // MULTIPLE_DELETE_CLIENTS: (state) => {
  //   for (let i = 0; i < state.clientChecked.length; i++) {
  //     let j = state.clientSelected.indexOf(state.clientChecked[i]);
  //     if (j > -1) {
  //       state.clientSelected.splice(j, 1);
  //     }
  //   }
  //   state.clientChecked = [];
  // },
  SAVE_ATTENTION: (state, data) => {
    state.attention = data;
  },
  SAVE_INSPECT: (state, { key, data }) => {
    state.inspectSummary[key] = data;
  },
  SAVE_RECTIFY: (state, { key, data }) => {
    state.rectifySummary[key] = data;
  },
};

const actions = {
  loadSource(context) {
    context.dispatch("loadInspectSource");
    context.dispatch("loadRectifySource");
  },
  loadInspectSource({ commit, rootGetters }) {
    let minDate =
      new Date(new Date().getTime() - 365 * 24 * 3600000)
        .toLocaleDateString()
        .replace(/\//g, "-") + " 00:00:00";
    let maxDate =
      new Date().toLocaleDateString().replace(/\//g, "-") + " 23:59:59";
    let filter = [
      {
        keys: ["begin"],
        operator: "<=",
        values: [[maxDate]],
      },
      {
        keys: ["time"],
        operator: ">=",
        values: [[minDate]],
      },
    ];
    // console.log(filter);
    axios
      .post("api/query_inspect_record/", {
        user: rootGetters.userbase,
        filter,
      })
      .then((response) => {
        if (response.data.status === 0) {
          commit("SET_INSPECT_SOURCE", response.data.content);
        } else {
          console.log(response);
        }
      })
      .catch((exception) => {
        console.log(exception);
      });
  },
  loadRectifySource({ commit, rootGetters }) {
    let startDate = new Date();
    let endDate = new Date(startDate.getTime() - 24 * 3600000 * 999);
    let startDateString =
      startDate.getFullYear() +
      "-" +
      (startDate.getMonth() + 1) +
      "-" +
      startDate.getDate() +
      " 23:59:59";
    let endDateString =
      endDate.getFullYear() +
      "-" +
      (endDate.getMonth() + 1) +
      "-" +
      endDate.getDate() +
      " 00:00:00";
    let userbase = rootGetters.userbase;
    console.log(userbase);
    axios
      .post("api/query_rectify_record_short/", {
        user: userbase,
        filter: {
          startTime: endDateString,
          endTime: startDateString,
        },
      })
      .then((response) => {
        console.log(response.data);
        if (response.data.status === 0) {
          commit("SET_RECTIFY_SOURCE", response.data.content);
        } else {
          console.log("query_rectify_record_short:\n");
          console.log(response.data);
        }
      })
      .catch((exception) => {
        console.log("query_rectify_record_short:\n");
        console.log(exception);
      });
    // inspect
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
